﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace DBLClient.DBLForm
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            this.IsMdiContainer = true;//多文档结构（MDI）必须设置这个

            if(!OnlineUserInfo.LEVEL.Equals("ADMIN"))
            {
                ToolStripMenuItemAdminManage.Enabled = false;
                ToolStripMenuItemDBLNoConfig.Enabled = false;
            }
            //this.Text = adminname + "已经登录";
        }

        private void OpenWindow(string ChildTypeString)
        {

            Form myChild = null;

            if (!ContainMDIChild(ChildTypeString))
            {

                // Get current process assembly

                Assembly assembly = Assembly.GetExecutingAssembly();



                // Create data type using type string

                Type typForm = assembly.GetType(ChildTypeString);



                // Create object using type's "InvokeMember" method

                Object obj = typForm.InvokeMember(

                    null,

                    BindingFlags.DeclaredOnly |

                    BindingFlags.Public | BindingFlags.NonPublic |

                    BindingFlags.Instance | BindingFlags.CreateInstance,

                    null,

                    null,

                    null);



                // Show child form 

                if (obj != null)
                {

                    myChild = obj as Form;

                    myChild.MdiParent = this;

                    myChild.Show();

                    myChild.Focus();

                }

            }

        }



        /// <summary>

        /// Search mdi child form by specific type string

        /// </summary>

        /// <param name="ChildTypeString"></param>

        /// <returns></returns>

        private bool ContainMDIChild(string ChildTypeString)
        {

            Form myMDIChild = null;

            foreach (Form f in this.MdiChildren)
            {

                if (f.GetType().ToString() == ChildTypeString)
                {

                    // found it 

                    myMDIChild = f;

                    break;

                }

            }



            // Show the exist form

            if (myMDIChild != null)
            {

                myMDIChild.TopMost = true;

                myMDIChild.Show();

                myMDIChild.Focus();

                return true;

            }

            else

                return false;

        }


        private void ToolStripMenuItemSearch_Click(object sender, EventArgs e)
        {
            OpenWindow(typeof(SearchUserForm).ToString());
        }

        private void ToolStripMenuItemOLastCode_Click(object sender, EventArgs e)
        {
            OpenWindow(typeof(DBLNoLast3CodeForm).ToString());
        }

        private void ToolStripMenuItemUserLevel_Click(object sender, EventArgs e)
        {
            OpenWindow(typeof(EditUserLevelForm).ToString());
        }

        private void ToolStripMenuItemAdmin_Click(object sender, EventArgs e)
        {
            OpenWindow(typeof(EditAdminForm).ToString());
        }

        private void ToolStripMenuItemCSLastCode_Click(object sender, EventArgs e)
        {
            OpenWindow(typeof(DBLNoLast4CodeForm).ToString());
        }

        private void ToolStripMenuItemPLastCOde_Click(object sender, EventArgs e)
        {
            OpenWindow(typeof(DBLNoLast6CodeForm).ToString());
        }

        private void ToolStripMenuItemHotWordAndGuessWord_Click(object sender, EventArgs e)
        {
            OpenWindow(typeof(EditGuessWordForm).ToString());
        }
    }
}
